package com.helwen.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author heqiwen
 * @title: SensitiveWordUtils
 * @projectName mall_qdg
 * @description: TODO
 * @date 2020/11/1217:18
 */
public class SensitiveWordUtils {


    /**
     * @param result
     * @return
     */
    public static Object apply(Object result) {
        if (result == null) {
            return null;
        }

        objectParse(result);

        return result;
    }



    /**
     * @param obj
     */
    public static void objectParse(Object obj) {
        List<Field> allField = findAllField(obj);
        for (Field field : allField) {
            field.setAccessible(true);
            Class<?> typeClazz = field.getType();
            matchFieldType(obj, field, typeClazz);
        }
    }

    public static List<Field> findAllField(Object object){
        List<Field> result = new ArrayList<>();

        Class<?> clazz = object.getClass();
        Field[] Fields = clazz.getDeclaredFields();
        result.addAll(Arrays.asList(Fields));
        while (true) {
            clazz = clazz.getSuperclass();
            if (clazz == Object.class) {
                break;
            }

            Field[] declaredFields = clazz.getDeclaredFields();
            result.addAll(Arrays.asList(declaredFields));
        }
        return result;
    }


    /**
     * @param obj
     * @param field
     * @param clazz
     */
    public static <T> void matchFieldType(Object obj, Field field, T clazz) {
        try {
            T param = (T) field.get(obj);
            if(param == null){
                return;
            }

            /*if (clazz == List.class) {
                List p = (List)param;
                for (Object o : p) {
                    objectParse(o);
                }
            } else */if (clazz == String.class) {
                String s=(String)param;
                String name = field.getName();
//                if(specialCharacters(s)){
//                    throw new IllegalAccessException("属性："+name+"中含有特殊字符");
//                }
            } else if (clazz == Map.class) {
                Map map = (Map)param;
                for (Object o : map.keySet()) {
                    objectParse(o);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param object
     * @param field
     * @param param
     * @throws IllegalAccessException
     */
    public static void setValue(Object object, Field field, Object param) throws IllegalAccessException {
        if(!field.isAccessible()){
            throw new IllegalAccessException("modify the field fail.");
        }

        field.set(object, param);
    }

    /*
     * 判断字符串中是否有特殊字符
     * 有返回true 没有false
     *
     * */
    public static boolean htmlCharacters(String stb) {
        String regEx = "<[^>]+>";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(stb);
        System.out.println(m.find());
        return m.find();
    }

    // 定义一些特殊字符的正则表达式 如：
    public static boolean scriptCharacters(String stb) {
        String regEx = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(stb);
        System.out.println(m.find());
        return m.find();
    }

    // 定义一些特殊字符的正则表达式 如：
    public static boolean specialCharacters(String stb) {
        String regEx = "[`~@#$%^&*()=|{}''\\[\\].<>/~！@#￥%……&*——+|{}【】]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(stb);
        System.out.println(m.find());
        return m.find();
    }
}
